package org.ims.inv.service.system.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.system.DAOAppUserProfile;
import org.ims.inv.model.system.ModelAppUser;
import org.ims.inv.model.system.ModelAppUserProfile;
import org.ims.inv.service.system.ServiceAppUserProfile;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilCollection;
import cn.trymore.core.util.UtilString;

/**
 * Service: 用户资料
 * 
 * @author x36zhao
 *
 */
public class ServiceAppUserProfileImpl
extends ServiceGenericImpl<ModelAppUserProfile> implements ServiceAppUserProfile
{
	/**
	 * The repository of user profile
	 */
	private DAOAppUserProfile daoAppUserProfile;
	
	/**
	 * The sole constructor
	 * 
	 * @param dao
	 */
	public ServiceAppUserProfileImpl(DAOAppUserProfile dao)
	{
		super(dao);
		this.daoAppUserProfile = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppUserProfile#findProfileByAccount(java.lang.String)
	 */
	@Override
	public ModelAppUserProfile findProfileByAccount (String accountId) throws ServiceException
	{
		if (UtilString.isNotEmpty(accountId))
		{
			ModelAppUserProfile criteria = new ModelAppUserProfile();
			ModelAppUser account = new ModelAppUser();
			account.setId(accountId);
			criteria.setAccount(account);
			
			List<ModelAppUserProfile> profiles = this.getByCriterion(criteria);
			if (UtilCollection.isNotEmpty(profiles))
			{
				return profiles.get(0);
			}
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelAppUserProfile domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAppUserProfile.class);
		
		if (domain != null)
		{
			if (UtilString.isNotEmpty(domain.getFullName()))
			{
				criteria.add(Restrictions.like("fullName", domain.getFullName(), MatchMode.ANYWHERE));
			}
			
			if (domain.getDepartment() != null)
			{
				criteria.createCriteria("department").add(Restrictions.eq("id", domain.getDepartment().getId()));
			}
			
			if (domain.getEducation() != null)
			{
				criteria.createCriteria("education").add(Restrictions.eq("id", domain.getEducation().getId()));
			}
			
			if (domain.getJobTitle() != null)
			{
				criteria.createCriteria("jobTitle").add(Restrictions.eq("id", domain.getJobTitle()));
			}
			
			if (domain.getAccount() != null)
			{
				criteria.createCriteria("account").add(Restrictions.eq("id", domain.getAccount().getId()));
			}
			
		}
		
		return criteria;
	}
	
	public DAOAppUserProfile getDaoAppUserProfile()
	{
		return daoAppUserProfile;
	}

	public void setDaoAppUserProfile(DAOAppUserProfile daoAppUserProfile)
	{
		this.daoAppUserProfile = daoAppUserProfile;
	}

}
